package com.woniuxy.oa.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.woniuxy.oa.entity.Employee;
import com.woniuxy.oa.exception.ServiceException;
import com.woniuxy.oa.mapper.LoginMapper;
import com.woniuxy.oa.service.LoginService;
import com.woniuxy.oa.service.impl.LoginServiceImpl;

@Controller
public class LoginController {
			
	@Autowired
	@Qualifier("LoginService")
	LoginService loginService;
	
	//获取验证码的方法
	@RequestMapping("/loginCode")
	public void generateLoginPic(HttpServletRequest request,HttpServletResponse response) {
		//创建随机字符数组
		char[] chs= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890".toCharArray();
		//通知客户端以图片的方式发送过去的数据
		response.setHeader("Content-Type", "image/jpg");
		//创建image对象
		BufferedImage image=new BufferedImage(100, 50, BufferedImage.TYPE_INT_RGB);
		Graphics g=image.getGraphics();
		//验证码图片背景颜色
		Color co=new Color(200,200,255);
		g.setColor(co);
		//绘制边框圆弧
		g.fillRect(0, 0, 100, 50);
		//保存验证码字符
		StringBuilder sb=new StringBuilder();
		int index=0;
		Random rand=new Random();
		for(int i=0;i<5;++i) {
			//获取随机下标
			index=rand.nextInt(chs.length);
			//随机颜色
			g.setColor(new Color(rand.nextInt(88),rand.nextInt(210),rand.nextInt(150)));
			//设置字体
			Font font=new Font("Fixedsys", Font.CENTER_BASELINE, 32);
			g.setFont(font);
			//绘制干扰线
		//	g.drawLine(30, 40,50, 60);
			//画出字符
			g.drawString(chs[index]+"", 15*i+3, 32);
			sb.append(chs[index]);
		}
		
		//将字符串存入session
		request.getSession().setAttribute("loginCode", sb.toString());
		System.out.println(sb.toString());
		try {
			ImageIO.write(image, "jpg", response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	@RequestMapping("/login1")
	public String login(Model model,String account,String password,String logincode,HttpSession session) {
		//判断是否已经登录
		
		if(session.getAttribute("user")!=null ) {
			return "/system/index/index";
		}
		//先判断验证码是否正确。
		if(!logincode.equalsIgnoreCase((String) session.getAttribute("loginCode"))) {
			model.addAttribute("loginCode", "验证码不正确");
			//不正确回到登录界面，并展示信息
			return "login";
		}
		Employee employee ;
		try {
			//Service层在没有查到用户的时候抛出异常
			 employee = loginService.selectEmployeeByAccount(account);
			//判断密码
			if(!employee.getPassword().equals(password)) {
				model.addAttribute("msg", "密码错误");
				return "login";
			}
		} catch (ServiceException e) {
			//将异常信息存入，在前端展示
			model.addAttribute("msg", e.getMessage());
			return "login";
		}
		//登陆成功后将用户名存入session中
		session.setAttribute("user", employee);
		//登录成功后获取当前时间在页面展示
		DateFormat df=new SimpleDateFormat("yyyy/MM/dd");
		String currentTime=df.format(new Date());
		session.setAttribute("currentTime", currentTime);
	//	System.out.println((Employee)session.getAttribute("user"));
		return "/system/index/index";
	}
	
	
	
	
	@RequestMapping("/exit")
	public String exit(HttpSession session) {
		session.removeAttribute("user");
		return "login";
	} 
	
	@RequestMapping("/modifyPassword")
	public String modifyPassword() {
		
		return "modifyPassword";
	}
	
	
	
	
	@RequestMapping("/changePassword")
	public String changePassword(HttpSession session,
								String oldPassword,String password,String rpassword,Model model) {
		Employee employee=(Employee)session.getAttribute("user");
		if(!oldPassword.equals(employee.getPassword())) {
			model.addAttribute("msg", "原密码错误");
			return "modifyPassword";
		}
		if(!password.equals(rpassword)) {
			model.addAttribute("msg","两次密码不一致");
			return "modifyPassword";		
		}
		String account=employee.getUsername();
		loginService.modifyPassword(rpassword, account);
		return "/system/index/index";
	}
	
	
	
	
	
	
	
	
}
